package com.gwcm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwcm.exception.BusinessException;
import com.gwcm.mapper.MemberCardMapper;
import com.gwcm.mapper.UserMapper;
import com.gwcm.mapper.UserRoleMapper;
import com.gwcm.model.entity.MemberCard;
import com.gwcm.model.entity.User;
import com.gwcm.model.entity.UserMemberCard;
import com.gwcm.service.UserMemberCardService;
import com.gwcm.mapper.UserMemberCardMapper;
import com.yhz.commonutil.common.ErrorCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* @author 15375
* @description 针对表【user_member_card(用户会员卡关联表)】的数据库操作Service实现
* @createDate 2023-03-21 16:19:38
*/
@Service
public class UserMemberCardServiceImpl extends ServiceImpl<UserMemberCardMapper, UserMemberCard>
    implements UserMemberCardService{
    @Resource
    private MemberCardMapper memberCardMapper;
    @Resource
    private UserMemberCardMapper userMemberCardMapper;

    /**
     * 新增顾客和会员卡关联
     * @param userMemberCard
     */
    @Override
    public void saveUserMemberCard(UserMemberCard userMemberCard){
        MemberCard memberCard = memberCardMapper.selectMemberCardById(userMemberCard.getMemberCardId());
        Integer validTime = memberCard.getValidTime();
        Integer validMonth = validTime/30;
        //如果用户购买过会员卡
        if(checkUserIdMemberCardIdUnique(userMemberCard.getUserId())){
            //userMemberCard =userMemberCardMapper.selectUserMemberCardByUserIdMemberCardId(userMemberCard.getUserId(), userMemberCard.getMemberCardId());
            //Date endTime = userMemberCard.getEndTime();
            //stepMonth(endTime, validMonth);
            //userMemberCard.setEndTime(endTime);
            throw new BusinessException(ErrorCode.SAVE_ERROR);
        }
        else {//如果用户未购买过会员卡
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            Date date = new Date();
            System.out.println(df.format(date));   // 当前系统时间
            Date endTime = stepMonth(date, validMonth);
            System.out.println("截止日期：" + df.format(endTime));
            userMemberCard.setEndTime(endTime);
        }
        int insert = baseMapper.insert(userMemberCard);
        if (insert < 1) {
            throw new BusinessException(ErrorCode.SAVE_ERROR);
        }
    }

    /**
     * 增加截止日期
     * @param sourceDate
     * @param month
     * @return
     */
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);
        return c.getTime();
    }

    /**
     * 检查用户会员卡关联中用户是否唯一
     * @param userId
     * @return
     */
    public boolean checkUserIdMemberCardIdUnique(Long userId){
        int count = userMemberCardMapper.checkUserIdUnique(userId);
        return count > 0 ? true : false;
    }

}




